//
//  TopOfBodyGV.swift
//  demo01
//
//  Created by babybus on 2025/7/7.
//
import Foundation
import UIKit
import SnapKit
import Alamofire

class TopOfBodyGVView:UIView{
    //全局变量
    private var shellImag:UIImageView = UIImageView(image: UIImage(named: "贝壳珍珠"))
    private var leftButton:UIButton = UIButton()
    private var rightButton:UIButton = UIButton()
    private var bubbles:[BubbleStack]=[]
    private let api = "/growthvalue/bubbles"
    //初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        loadData(userId:1)
        setupUI()
        setButton()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        self.addSubview(shellImag)
        self.addSubview(leftButton)
        self.addSubview(rightButton)
        shellImag.snp.makeConstraints { make in
            make.width.equalTo(115)
            make.height.equalTo(100)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().inset(5)
        }
        leftButton.snp.makeConstraints { make in
            make.width.height.equalTo(40)
            make.bottom.equalToSuperview().inset(33)
            make.left.equalToSuperview().inset(79)
        }
        rightButton.snp.makeConstraints { make in
            make.width.height.equalTo(40)
            make.bottom.equalToSuperview().inset(33)
            make.right.equalToSuperview().inset(77)
        }
    }
    func setButton(){
        leftButton.setImage(UIImage(named: "leftButtonGV"), for: .normal)
        rightButton.setImage(UIImage(named: "rightButtonGV"), for: .normal)
        leftButton.addTarget(self, action: #selector(previousLevel), for: .touchUpInside)
        rightButton.addTarget(self, action: #selector(nextLevel), for: .touchUpInside)
    }
    weak var delegateLevel:LevelDelegate?
    //委托协议
    protocol LevelDelegate:AnyObject {
        func nextLevel()
        func previousLevel()
    }
    @objc func nextLevel(){
        self.delegateLevel?.nextLevel()
    }
    @objc func previousLevel(){
        self.delegateLevel?.previousLevel()
    }
    //获取数据
    func loadData(userId:Int){
        let url = AllUrl.LH_URL + api + "?userId=\(userId)"
        AF.request(url,method: .get)
            .validate(statusCode: 200..<300)
            .responseDecodable(of:[BubbleInfo].self){ [weak self] response in
                switch response.result{
                case .success(let bubbleList):
                    DispatchQueue.main.async {
                        for info in bubbleList{
                            let bubble = BubbleStack()
                            bubble.configure(img: info.img, number: info.number, title: info.title)
                            self?.bubbles.append(bubble)
                        }
                        self?.placeBubble()
                    }
                case .failure(let error):
                    print("错误 -- GVCollection: \(error.localizedDescription)")
                }
            }
    }
    func placeBubble(){
        var n = 0 , sum = bubbles.count
        while n<sum && n<4{
            self.addSubview(bubbles[n])
            bubbles[n].snp.makeConstraints { make in
                make.width.equalTo(60)
                make.height.equalTo(75)
            }
            switch n {
            case 0:
                bubbles[n].snp.makeConstraints { make in
                    make.top.equalToSuperview().inset(41)
                    make.left.equalToSuperview().inset(19)
                }
            case 1:
                bubbles[n].snp.makeConstraints { make in
                    make.top.equalToSuperview().inset(8)
                    make.left.equalToSuperview().inset(89)
                }
            case 2:
                bubbles[n].snp.makeConstraints { make in
                    make.top.equalToSuperview().inset(0)
                    make.left.equalToSuperview().inset(159)
                }
            case 3:
                bubbles[n].snp.makeConstraints { make in
                    make.top.equalToSuperview().inset(9)
                    make.left.equalToSuperview().inset(229)
                }
            default:
                print("n>=5")
            }
            n+=1
        }
        let redBubble = RedBubbleStack()
        self.addSubview(redBubble)
        redBubble.snp.makeConstraints { make in
            make.width.height.equalTo(78)
            make.right.equalToSuperview()
            make.top.equalToSuperview().inset(37)
        }
    }
}

